package com.ynet.finmall.consumer.inmanage.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ynet.finmall.consumer.inmanage.request.ExampleRequest;
import com.ynet.finmall.consumer.inmanage.response.ExampleResponse;
import com.ynet.finmall.inmanage.model.ImUserAndRole;
import com.ynet.finmall.inmanage.server.ExampleEDASServer;


/**
 * 注解的Handler
 */
@Controller
@ResponseBody
public class ExampleController {

	private static final Logger logger = LoggerFactory.getLogger(ExampleController.class);

	@Value("${redis.ip}")
	private String redisIp;
	
	@Value("${redis.post:8080}")
	private String redisPost;
	
	@Autowired
	@Qualifier("exampleEDASServer")
	private ExampleEDASServer exampleEDASServer;
	
    @RequestMapping("/query")
    public void queryItem(HttpServletRequest request, HttpServletResponse response) throws IOException {

    	logger.debug("redis ip: " + redisIp + " redis post: " + redisPost);
    	
        PrintWriter out = response.getWriter();
        out.println("redis ip: " + redisIp);
        out.println("redis post: " + redisPost);
        out.flush();
        out.close();
    }

    @RequestMapping("/exampleDev")
    public ExampleResponse exampleDev(@RequestBody ExampleRequest exampleRequest) throws IOException {
    	String id = exampleRequest.getId();
    	
    	logger.debug("id: " + id);
    	
    	ImUserAndRole imUserAndRole = exampleEDASServer.exampleDev(id);
    	
    	if(imUserAndRole == null){
    		imUserAndRole = new ImUserAndRole();
    	}
    	JSON json = (JSON) JSONObject.toJSON(imUserAndRole);
    	ExampleResponse exampleResponse = JSONObject.parseObject(json.toJSONString(), ExampleResponse.class);

        return exampleResponse;
        
    }
    
}
